//
//  JYJVCOfAnchorInfoViewController.m
//  HappyListen
//
//  Created by 冀永金 on 16/9/1.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJVCOfAnchorInfoViewController.h"
#import "Masonry.h"
#import "JYJTAMacro.h"
#import "BaseLabel.h"
#import "BaseCollectionView.h"
#import "JYJAncCollectionViewCell.h"
#import "JYJAnchorDataCollectionViewCell.h"
#import "JYJSpecialListModel.h"
#import "JYJNetWorkParser.h"
#import "UIImageView+WebCache.h"
#import "JYJAnchorColumnCollectionViewCell.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"
#import "JYJMusicMiniView.h"
#import "JYJVCOfAnthorHeadImage.h"
@interface JYJVCOfAnchorInfoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIImageView *backImage;
@property (nonatomic, strong) UIImageView *genderImage;
@property (nonatomic, strong) BaseCollectionView *smallCollection;
@property (nonatomic, strong) BaseCollectionView *bigCollection;
@property (nonatomic, strong) UIView *redView;
@property (nonatomic, assign) NSInteger labelIndex;
@property (nonatomic, strong) NSIndexPath *oldIndexPath;
@property (nonatomic, strong) NSDictionary *infoDic;
@property (nonatomic, strong) BaseLabel *nameLabel;
@property (nonatomic, strong) UIImageView *imageOfPeople;
@property (nonatomic, strong) BaseLabel *signatureLabel;
@end

@implementation JYJVCOfAnchorInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.oldIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    self.view.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    [self configSubViews];
   self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self createSamllCollection];
    [self creatBigCollection];
    [self createRedView];
    [self createKVOAction];
    [self handleData];
    // Do any additional setup after loading the view.
}
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)handleData {
    if (self.listModel) {
        NSString *str1 = @"json=%7B%22user_id%22%3A%22";
        NSString *string = [NSString stringWithFormat:@"%@",self.listModel.content_id];
        NSString *str3 = @"%22%2C%22session%22%3A%7B%22uid%22%3A%22%22%2C%22sid%22%3A%22%22%7D%7D";
        string = [[str1 stringByAppendingString:string]stringByAppendingString:str3];
        [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/social/userinfo.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
            self.infoDic = [result[@"data"] objectForKey:@"info"];
            self.nameLabel.text = [self.infoDic objectForKey:@"nickname"];
            self.navigationItem.title = [NSString stringWithFormat:@"%@的主页",[self.infoDic objectForKey:@"nickname"]];
            self.signatureLabel.text = [self.infoDic objectForKey:@"signature"];
            [self.imageOfPeople sd_setImageWithURL:[NSURL URLWithString:[self.infoDic objectForKey:@"userimg"]] placeholderImage:PLACEHOLDERIMAGE];
            NSString *gender = [self.infoDic objectForKey:@"gender"];
            if ([gender isEqualToString:@"1"]) {
                self.genderImage.image = [UIImage imageNamed:@"icon_sex_0"];
            }else {
                self.genderImage.image = [UIImage imageNamed:@"icon_sex_1"];
            }
            [self.smallCollection reloadData];
            [self.bigCollection reloadData];
        } andFailureBlock:^(NSError *error) {
            
        }];
        
    }else {

        NSString *str1 = @"json=%7B%22user_id%22%3A%22";
        NSString *string = [NSString stringWithFormat:@"%@",self.dic[@"item_managerid"]];
        NSString *str3 = @"%22%2C%22session%22%3A%7B%22uid%22%3A%22%22%2C%22sid%22%3A%22%22%7D%7D";
        string = [[str1 stringByAppendingString:string]stringByAppendingString:str3];
        [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/social/userinfo.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
            self.infoDic = [result[@"data"] objectForKey:@"info"];
            self.nameLabel.text = [self.infoDic objectForKey:@"nickname"];
            self.navigationItem.title = [NSString stringWithFormat:@"%@的主页",[self.infoDic objectForKey:@"nickname"]];
            self.signatureLabel.text = [self.infoDic objectForKey:@"signature"];
            [self.imageOfPeople sd_setImageWithURL:[NSURL URLWithString:[self.infoDic objectForKey:@"userimg"]] placeholderImage:PLACEHOLDERIMAGE];
            NSString *gender = [self.infoDic objectForKey:@"gender"];
            if ([gender isEqualToString:@"1"]) {
                self.genderImage.image = [UIImage imageNamed:@"icon_sex_0"];
            }else {
                self.genderImage.image = [UIImage imageNamed:@"icon_sex_1"];
            }
            [self.smallCollection reloadData];
            [self.bigCollection reloadData];
        } andFailureBlock:^(NSError *error) {
            
        }];

    }
}
- (void)configSubViews {
    self.backImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mohu "]];
    [self.view addSubview:_backImage];
    self.backImage.frame = CGRectMake(0, 0,SCREEN_WIDTH , 220);
    _backImage.userInteractionEnabled = YES;
    UIImageView *imageOfHead = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"find_anchor_cover"]];
    [self.backImage addSubview:imageOfHead];
    imageOfHead.userInteractionEnabled = YES;
    [imageOfHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(30);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
        make.left.equalTo(self.view.mas_left).with.offset(SCREEN_WIDTH / 2 - 40);
    }];
    self.backImage.userInteractionEnabled = YES;
    self.imageOfPeople = [[UIImageView alloc]init];
    [self.backImage addSubview:_imageOfPeople];
    self.imageOfPeople.userInteractionEnabled = YES;
    [self.imageOfPeople mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(32);
        make.width.mas_equalTo(76);
        make.height.mas_equalTo(76);
        make.centerX.mas_equalTo(imageOfHead.mas_centerX);
    }];
    self.imageOfPeople.layer.cornerRadius = 38;
    self.imageOfPeople.clipsToBounds = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleAction)];
    [self.imageOfPeople addGestureRecognizer:tap];
    
    self.nameLabel = [[BaseLabel alloc]init];
    [self.backImage addSubview:_nameLabel];
    _nameLabel.textAlignment = 1;
    _nameLabel.textColor = [UIColor whiteColor];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageOfHead.mas_bottom).with.offset(15);
        make.left.equalTo(self.view.mas_left).with.offset(50);
        make.right.equalTo(self.view.mas_right).with.offset(-50);
        make.height.mas_equalTo(40);
    }];
    self.genderImage = [[UIImageView alloc]init];
    [self.backImage addSubview:_genderImage];
    [self.genderImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(65);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
        make.left.equalTo(imageOfHead.mas_right).with.offset(10);
    }];
    UIImageView *coronaImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"zhubo_header"]];
    [self.backImage addSubview:coronaImage];
    [coronaImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(30);
        make.left.equalTo(imageOfHead.mas_right).with.offset(-11);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    self.signatureLabel = [[BaseLabel alloc]init];
    [self.backImage addSubview:_signatureLabel];
    [_signatureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nameLabel.mas_bottom).with.offset(5);
        make.left.equalTo(self.view.mas_left).with.offset(20);
        make.right.equalTo(self.view.mas_right).with.offset(-20);
        make.height.mas_equalTo(30);
    }];
    _signatureLabel.textAlignment = 1;
    _signatureLabel.textColor = [UIColor lightGrayColor];
    
}
- (void)createSamllCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 2, 50);
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollPositionCenteredHorizontally;
    self.smallCollection = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 220, SCREEN_WIDTH, 50) collectionViewLayout:layout];
    [self.view addSubview:_smallCollection];
    self.smallCollection.delegate = self;
    self.smallCollection.dataSource = self;
    [self.smallCollection registerClass:[JYJAncCollectionViewCell class] forCellWithReuseIdentifier:@"Small"];
}
- (void)creatBigCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 273);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    self.bigCollection = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 273, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 273) collectionViewLayout:layout];
    [self.view addSubview:_bigCollection];
    self.bigCollection.delegate = self;
    self.bigCollection.dataSource = self;
    self.bigCollection.pagingEnabled = YES;
    [self.bigCollection registerClass:[JYJAnchorDataCollectionViewCell class] forCellWithReuseIdentifier:@"Big"];
    [self.bigCollection registerClass:[JYJAnchorColumnCollectionViewCell class] forCellWithReuseIdentifier:@"Column"];
    self.bigCollection.showsHorizontalScrollIndicator = NO;
    self.bigCollection.backgroundColor = [UIColor whiteColor];
    self.bigCollection.scrollEnabled = NO;
}
- (void)createRedView {
    self.redView = [[UIView alloc]initWithFrame:CGRectMake(0, 270, SCREEN_WIDTH / 2, 3)];
    [self.view addSubview:_redView];
    _redView.backgroundColor = [UIColor redColor];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _smallCollection) {
        JYJAncCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Small" forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.nameLabel.text = @"资料";
        }else {
            cell.nameLabel.text = [NSString stringWithFormat:@"栏目(%@)",[self.infoDic objectForKey:@"lanmucnt"]];
        }
        if (indexPath.row == _labelIndex) {
            cell.nameLabel.textColor = [UIColor whiteColor];
        }else {
            cell.nameLabel.textColor = [UIColor lightGrayColor];
        }
        if (indexPath.row == _oldIndexPath.item) {
            cell.nameLabel.textColor = [UIColor whiteColor];
        }else {
            cell.nameLabel.textColor = [UIColor lightGrayColor];
        }
        return cell;
        
    }else {
        if (indexPath.item == 0) {
            JYJAnchorDataCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Big" forIndexPath:indexPath];
            cell.infoDic = self.infoDic;
            return cell;
            
        }else {
            JYJAnchorColumnCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Column" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor whiteColor];
            __weak typeof(self) weakself= self;
            cell.handleAction = ^ (JYJVCOfFeaturedTopThreeDetails *VCOfdetails) {
                weakself.tabBarController.tabBar.hidden = YES;
                weakself.hidesBottomBarWhenPushed = YES;
                [weakself.navigationController pushViewController:VCOfdetails animated:YES];
                weakself.hidesBottomBarWhenPushed = NO;
            };
            cell.listModel = self.listModel;
            cell.dic = self.dic;
            return cell;
        }
    }
}
// 添加观察者
- (void)createKVOAction {
    [self.bigCollection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    CGFloat X = [[change objectForKey:@"new"]CGPointValue].x;
    CGFloat redX = X / 2;
    self.redView.transform = CGAffineTransformMakeTranslation(redX, 0);
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.bigCollection) {
        //当前大Collection的第几个item
        NSInteger currentItem = scrollView.contentOffset.x / SCREEN_WIDTH;
        // 根据第几个item获取当前的indexPath
        NSIndexPath *currentIndexPath = [NSIndexPath indexPathForItem:currentItem inSection:0];
        // 根据indexpaht获取当前的标题的Collection
        JYJAncCollectionViewCell *cell = (JYJAncCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:currentIndexPath];
        //字体变色
        cell.nameLabel.textColor = [UIColor whiteColor];
        // 恢复字体颜色
        NSIndexPath *indexPathPast = [NSIndexPath indexPathForItem:_labelIndex inSection:0];
        //加判防止滑一点点就就开，并没有划入新页面，字体就变色了
        if (currentIndexPath != indexPathPast) {
            JYJAncCollectionViewCell *cell = (JYJAncCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:indexPathPast];
            cell.nameLabel.textColor = [UIColor lightGrayColor];
        }
        //记录当前的item 和 划过的indexPath
        self.labelIndex = currentItem;
        self.oldIndexPath = currentIndexPath;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.smallCollection) {
        [self.bigCollection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
       JYJAncCollectionViewCell *cell = (JYJAncCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:indexPath];
        cell.nameLabel.textColor = [UIColor whiteColor];
        JYJAncCollectionViewCell*pastCell = (JYJAncCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:_oldIndexPath];
        if (indexPath != _oldIndexPath) {
            pastCell.nameLabel.textColor = [UIColor lightGrayColor];
        }
        self.oldIndexPath = indexPath;
        self.labelIndex = _oldIndexPath.item;
    }
}
-(void)dealloc {
    [self.bigCollection removeObserver:self forKeyPath:@"contentOffset"];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [JYJMusicMiniView shareMiniView].hidden = YES;
}
#pragma mark-------主播头像
- (void)handleAction {
    JYJVCOfAnthorHeadImage *head = [[JYJVCOfAnthorHeadImage alloc]init];
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:head animated:YES];
    
    head.imageDic = self.dic;
    head.dic = self.infoDic;
    self.hidesBottomBarWhenPushed = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
